package tree;

import java.io.File;

import perspectives.DataSource;
import perspectives.Property;
import perspectives.PropertyManager.OpenFile;
import perspectives.PropertyManager.Options;

public class TreeData extends DataSource{
	
	Tree tree;
	
	boolean valid;

	public TreeData(String name) {
		super(name);
		
		tree = null;
		valid = false;
		
		try {
			OpenFile f = new OpenFile();
			f.dialogTitle = "Open Graph File";
			f.extensions = new String[3];
			f.extensions[0] = "txt";
			f.extensions[1] = "xml";
			f.extensions[2] = "*";
			
			Property<OpenFile> p1 = new Property<OpenFile>("Tree File");
			p1.setValue(f);
			this.addProperty(p1);
			
			Options o = new Options();
			o.options = new String[2];
			o.options[0] = "Newick"; o.options[1] = "GraphML";
			o.selectedIndex = 0;
			
			Property<Options> p2 = new Property<Options>("Format");
			p2.setValue(o);
			this.addProperty(p2);
			
		} catch (Exception e) {
			e.printStackTrace();
		}	
	}

	@Override
	public <T> void propertyUpdated(Property p, T newvalue) {
		if (p.getName() == "Tree File")
		{
			Property format = this.getProperty("Format");
			
			tree = new Tree(new File(((OpenFile)newvalue).path), ((Options)format.getValue()).options[((Options)format.getValue()).selectedIndex]);
			
			this.setLoaded(true);
		}
		if (p.getName() == "Format")
		{
			String fs = ((Options)newvalue).options[((Options)newvalue).selectedIndex];
			
			if (fs.equals("GraphML"))
				((OpenFile)this.getProperty("Graph File").getValue()).currentExtension = 1;
			else if (fs.equals("Newick"))
				((OpenFile)this.getProperty("Graph File").getValue()).currentExtension = 0;	
		}
	}

	

	
	

}
